package com.ys.io.p4Properties集合;

import java.io.*;
import java.util.Properties;

/**
 * 描述: Properties的读与写
 *
 * @author CodeYang
 * @version 2020/12/31 15:59
 */
public class Properties的读与写 {

    public static void main(String[] args) {

        String  strPath = "day_24/tst.properties";

        // 1: Properties 的持久化 对象
        Properties properties = new Properties();
        properties.setProperty("01","01");
        properties.setProperty("02","02");
        properties.setProperty("ab","ab");

        try {
            //场景：将Properties集合中所有的键值对写入磁盘中
            properties.store(new FileOutputStream(strPath),"this is momments");
            System.out.println("写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 2 : Properties的读取 从磁盘到程序(内存) properties配置文件里面所有的键值对加载(读)到Properties集合中
        System.out.println("从磁盘读properties加载到Properties集合中" );
        try {
            // 使用缓冲流来读取
            // InputStreamReader缓冲流就是一个适配器  左边 字符流 右边字节流
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(strPath),"UTF-8"));
            Properties properties1 = new Properties();
            // 把缓冲流里的数据 给这个新properties1对象
            properties1.load(br);
            System.out.println(properties1);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}
